{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "52bdbf28",
   "metadata": {},
   "source": [
    "# 列表的基本用法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41aabcc6",
   "metadata": {},
   "source": [
    "# 1.增加的方法\n",
    "## 1.1 增加 append()  增加在list的末尾\n",
    "# names.append(1)\n",
    "# print(names)\n",
    "\n",
    "## 1.2 插入 insert() 可以指定位置增加元素\n",
    "names.insert(1,'yougege')\n",
    "print(\"insert之后的names：\",names)\n",
    "\n",
    "# 2. 删除 remove  和 pop\n",
    "#优势：如果删除list中多个元素时，remove比较合适\n",
    "## 2.1 remove()一般情况下 使用remove先用in做判断\n",
    "if 10001 in names:\n",
    "     names.remove(10001)\n",
    "print(\"remove删除的结果：\",names)\n",
    "\n",
    "\n",
    "## 2.2 pop() 删除位置为1的这个元素\n",
    "names.pop(-1)\n",
    "print(\"pop()方法后的names:\",names)\n",
    "\n",
    "\n",
    "## 3. index()索引 查看该元素如10001 在stu_id中第一个出现的位置索引\n",
    "print(stu_id.index(10001))\n",
    "\n",
    "## 4. clear()清除所有数据\n",
    "names.clear()\n",
    "print(\"清除names列表后的值：\",names)\n",
    "\n",
    "## 5.count()查看列表元素出现的个数，（简单应用）统计分数出现的次数\n",
    "print(\"count方法的结果:\",stu_id.count(10001))\n",
    "print(names)\n",
    "\n",
    "## 6. extend() 是给指定的list进行扩充\n",
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8eaf9c7",
   "metadata": {},
   "source": [
    "# 列表的高级方法\n",
    "### copy（）的使用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66a47550",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "867e49d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 赋值\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30eb3822",
   "metadata": {},
   "outputs": [],
   "source": [
    "# copy\n",
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f5901e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过切片实现复制\n",
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db0ba282",
   "metadata": {},
   "source": [
    "### 枚举："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d9df8c3",
   "metadata": {},
   "source": [
    "```\n",
    "#案例：\n",
    "stu_id = [10001,10002,10003,10004,10008,10001,10009,10010,10001]\n",
    "取出所有位置10001的index\n",
    "for i in stu_id:\n",
    "    # 查看stu_id的所有内容\n",
    "    if i == 10001:\n",
    "        # 判断如果元素为10001的时候,怎么取出索引值呢？\n",
    "        print()\n",
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    # print(index,item)\n",
    "    if item == 10001:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "855baf03",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
